#coding:utf-8
import sys, os
import json
sys.path.append("matchutils")
from utils import pygtrie 

from config import *

video_cate_words = {'电影':1, '电视剧':1, '电影版':1, '综艺':1,'纪录片':1, '动画':1, '动画片':1 }


# 卫视版　区分度不大
# 电影版
# 
"""
季／部：如果两个都有，则判断是否相同，否则根据年份
tv版／卫视版：等价
央视版／卫视版：不相同
语言：如果都有，则判断是否相同，一个则根据语言

"""

def is_japanese( s ):
    if isinstance(s, str):
        s = s.decode('utf-8', 'ignore')
    tot, num = 0, 0
    for ch in s:
        i = ord( ch ) 
        tot += 1
        if i >= 0x30A0 and i <= 0x30FF:
            num += 1
        elif i >= 0x3100 and i <= 0x312F:
            num += 1

    if num > 0 and num * 2 >= tot:
        return True

    return False


class WordTool(object):
    def __init__(self):
        self.low_words = {}
        self.cate_words = {}
        self.differentwords = {}
        self.bad_alias_words = {}
        f = open(fname_bad_alias, 'rb')
        for line in f:
            ####################################            
            line = line.strip()
            if line == '': continue
            if line.startswith('#'): continue
            line = line.lower()
            self.bad_alias_words[ line ] = 1
        f.close()

        f = open( fname_imp_word, 'rb')
        for line in f:
            ####################################            
            line = line.strip()
            if line == '': continue
            if line.startswith('#'): continue
            fields = line.split('\t')
            if len(fields) != 2: continue
            word, imp = fields
            
            self.differentwords[ word ] = imp
            ####################################            
        f.close()

        self.keywords_dic = pygtrie.CharTrie()
        f = open( dict_dir + "/keywords.txt", 'rb')
        for line in f:
            line = line.lower().strip()
            if line == '': continue
            line = line.decode('utf-8', 'ignore')
            self.keywords_dic[ line ] = 1
        f.close()

        self.hanDigs = {}
        self.hanDigs['一'] = 1
        self.hanDigs['二'] = 2
        self.hanDigs['三'] = 3
        self.hanDigs['四'] = 4
        self.hanDigs['五'] = 5
        self.hanDigs['六'] = 6
        self.hanDigs['七'] = 7
        self.hanDigs['八'] = 8
        self.hanDigs['九'] = 9
        self.hanDigs['十'] = 10
        self.hanDigs['十一'] = 11
        self.hanDigs['十二'] = 12
        self.hanDigs['十三'] = 13
        self.hanDigs['十四'] = 14
        self.hanDigs['十五'] = 15
        self.hanDigs['十六'] = 16
        self.hanDigs['十七'] = 17
        self.hanDigs['十八'] = 18
        self.hanDigs['十九'] = 19
        self.hanDigs['二十'] = 20
        self.hanDigs['二十一'] = 21
        self.hanDigs['二十二'] = 22
        self.hanDigs['二十三'] = 23
        self.hanDigs['二十四'] = 24
        self.hanDigs['二十五'] = 25
        self.hanDigs['二十六'] = 26
        self.hanDigs['二十七'] = 27
        self.hanDigs['二十八'] = 28
        self.hanDigs['二十九'] = 29
        self.hanDigs['三十'] = 30

        #同义词
        self.syn_words = {}
        f = open(fname_syn_word, 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            if line.startswith('#'): continue
            fields = line.split('\t')
            if len(fields) != 2: continue
            ori, dst = fields
            if ori not in self.syn_words:
                self.syn_words[ ori ] = []
            self.syn_words[ori].append( dst )
        f.close()

        #人名映射
        self.name_maps = {}
        f = open(fname_map_name, 'rb')
        for line in f:
            line = line.strip()
            if line == '': continue
            fields = line.split('\t')
            if len(fields) != 2:
                continue
            w1 = fields[0].strip().lower()
            w2 = fields[1].strip().lower()
            self.name_maps[w1] = w2
        f.close()

    def is_low(self, word):
        val = self.get_word_imps(word)
        if val is not None and val == 0:
            return True
        return False

    def get_word_imps(self, word ): 
        val = self.differentwords.get( word, None)
        if val is not None:
            val = int(val)
            
        return val
    def is_bad_alias(self, w ):
        if w in self.bad_alias_words:
            return True
        return False

    def get_han_num(self, word ):
        val = self.hanDigs( word, None )
        return val
    def get_peopple_map( self, name ):
        key = name.lower().strip()
        if key in self.name_maps:
            return self.name_maps[key]
        return name
    def get_syn_words(self, ori ):
        if ori not in self.syn_words:
            return []
        return self.syn_words[ori]
    def extract_keywords(self, text ):
        keywords = []
        if isinstance(text, str):
            text = text.decode('utf-8', 'ignore')
        if not text: 
            return []
        num = len(text) 
        i = 0
        while i < num:
            res, val = self.keywords_dic.longest_prefix( text[i:])
            if not res:
                i += 1
                continue
            i += len(res) 
            keywords.append( res.encode('utf-8', 'ignore') ) 
        return keywords


wordTool = WordTool()

if __name__ == "__main__":
    print wordTool.get_word_imps( '澳洲版')
    pass
